from sqlalchemy import Column, Integer, String, Text, Boolean, ForeignKey
from sqlalchemy.orm import Session
from typing import List, Optional, Any
from src.sqlite.database import Base, SessionLocal, engine

class Resume(Base):
    __tablename__ = "resumes"

    id = Column(Integer, primary_key=True, index=True)
    resume_thread_id = Column(String, nullable=False)
    resume_url = Column(String, nullable=False)
    parsed_content = Column(Text, nullable=True)
    match_report = Column(Text, nullable=True)
    is_matched = Column(Boolean, default=False)
    interview_questions = Column(Text, nullable=True)
    candidate_name = Column(String, nullable=True)

# 初始化数据库表（可放项目启动代码中）
def init_db():
    Base.metadata.create_all(bind=engine)
    print("✅ Tables created in:", engine.url)

# 保存简历
def save_resume(
    resume_thread_id: str,
    candidate_name: str,
    resume_url: str,
    parsed_content: str | None = None,
    match_report: str | None = None,
    is_matched: bool = False,
    interview_questions: str | None = None,
)-> int:
    with SessionLocal() as session:
        resume = Resume(
            resume_thread_id=resume_thread_id,
            resume_url=resume_url,
            parsed_content=parsed_content,
            match_report=match_report,
            is_matched=is_matched,
            interview_questions=interview_questions,
            candidate_name=candidate_name,
        )
        session.add(resume)
        session.commit()
        return resume.id

# 查询单个简历
def get_resume_by_id(session: Session, resume_id: int) -> Optional[Resume]:
    return session.query(Resume).filter(Resume.id == resume_id).first()

# 查询简历列表，支持分页
def list_resumes(session: Session, skip: int = 0, limit: int = 100) -> list[type[Resume]]:
    return session.query(Resume).offset(skip).limit(limit).all()

# 根据面试者名称模糊查询简历
def find_resumes_by_candidate_name(session: Session, name: str) -> list[type[Resume]]:
    return session.query(Resume).filter(Resume.candidate_name.ilike(f"%{name}%")).all()

# 更新简历内容
def update_resume(
    session: Session,
    resume_id: int,
    parsed_content: Optional[str] = None,
    match_report: Optional[str] = None,
    is_matched: Optional[bool] = None,
    interview_questions: Optional[str] = None,
    candidate_name: Optional[str] = None,
) -> type[Resume] | None:
    resume = session.query(Resume).filter(Resume.id == resume_id).first()
    if not resume:
        return None
    if parsed_content is not None:
        resume.parsed_content = parsed_content
    if match_report is not None:
        resume.match_report = match_report
    if is_matched is not None:
        resume.is_matched = is_matched
    if interview_questions is not None:
        resume.interview_questions = interview_questions
    if candidate_name is not None:
        resume.candidate_name = candidate_name
    session.commit()
    return resume

if __name__ == "__main__":

    # 先确保表已创建
    init_db()

    # 测试插入
    id = save_resume('1', "xxxx",'https://example.com/resume.pdf', '解析后的简历内容', '匹配报告', True, '面试问题', )
    print(f'id :{id}')
    # 测试查询
    session = SessionLocal()
    resumes = session.query(Resume).all()
    for r in resumes:
        print(r.resume_thread_id, r.resume_url, r.parsed_content, r.match_report, r.is_matched, r.interview_questions, r.candidate_name)
    session.close()